﻿
/*'***************************************************************************
'  Copyright (C) 2015, Intergraph Corporation.  All rights reserved.
'
'  Project: NamingRules
'
  Abstract:   The file contains an implementation of the boolean naming rule
'              for the Compartment object in Compartmentation UE.

'  Description:This name rule computes the compartment name based on the catalog part description and
'              adds '+' for the added boolean feature name and '-' for the subtracted boolean feature name if there are any features added to the compartment

'  Example:    If a compartment A is being created with B and C considerd as Boolean Add and Boolean subtract respectively.
'              Then, this name rule changes the name of B as '+ A' and C as '- A'
'
'  History:
'  Sravanya            10th March 2015               Creation
'****************************************************************************/
using Ingr.SP3D.Common.Middle;
using Ingr.SP3D.Common.Middle.Services;
using System;
using Ingr.SP3D.Compartmentation.Middle;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Ingr.SP3D.ReferenceData.Middle;

namespace Ingr.SP3D.Content.Compartmentation
{
    public class CompBoolNameRule : NameRuleBase
    {

        private const string strFormat = "{0:0000}";
        /// <summary>
        /// Creates a name for the object passed in. The name is based on the parents name and object name. It is assumed that all Naming Parents and the Object implement JNamedItem.
        /// The Naming Parents are added in AddNamingParents() of the same interface. Both these methods are called from the naming rule semantic.
        /// </summary>
        /// <param name="businessObject">The business object whose name is being computed. </param>
        /// <param name="parents">Naming parents of the business object, 
        /// i.e. other business objects which control the naming of the business object whose name is being computed.</param>
        /// <param name="activeEntity">The name rule active entity associated to the business object whose name is being computed.</param>
        public override void ComputeName(BusinessObject entity, ReadOnlyCollection<BusinessObject> parents, BusinessObject activeEntity)
        {
            try
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }
                if (activeEntity == null)
                {
                    throw new ArgumentNullException("activeEntity");
                }

                string strChildName = string.Empty, strLocation = string.Empty;
                long count;
                BusinessObject targetPart;

                strChildName = Convert.ToString(entity.GetPropertyValue("IJNamedItem", "Name"));

                if (!string.IsNullOrEmpty(strChildName))
                {
                    return;
                }

                RelationCollection relationHelper = entity.GetRelationship("CompartDefinition", "Part");

                if (relationHelper.TargetObjects.Count == 1)
                {
                    targetPart = relationHelper.TargetObjects[0];
                }
                else
                {
                    return;
                }

                Part part = (Part)targetPart;
                strChildName = part.PartDescription;
                //Returns the number of occurrence of a string in addtion to the LocationID
                GetCountAndLocationID(strChildName, out count, out strLocation);

                //Add LocationID, if available
                if (strLocation != string.Empty)
                {
                    strChildName = strChildName + "-" + strLocation + "-" + string.Format(strFormat, count);
                }
                else
                {
                    strChildName = strChildName + "-" + string.Format(strFormat, count);
                }

                entity.SetPropertyValue(strChildName, "IJNamedItem", "Name");
               // 'Set the name for boolean features
                SetNameForBoolFeatures(entity, strChildName);
            }
            catch (Exception e)
            {
                MiddleServiceProvider.ErrorLogger.Log("FrameRule.ComputeName: Error encountered (" + e.Message + ")");
            }
        }

        /// <summary>
        ///   All the Naming Parents that need to participate in an objects naming are added here to the collection. Dummy function which does nothing
        /// </summary>
        /// <param name="entity">The business object whose name is being computed. </param>
        public override Collection<BusinessObject> GetNamingParents(BusinessObject entity)
        {
            Collection<BusinessObject> oRetColl = new Collection<BusinessObject>();
            try
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }
            }
            catch (Exception e)
            {
                MiddleServiceProvider.ErrorLogger.Log("FrameRule.GetNamingParents: Error encountered (" + e.Message + ")");
            }

            return oRetColl;

        }

        
        /// <summary>
        ///'This function adds '+' for the added feature and '-' for the subtracted feature
        /// </summary>
        /// <param name="entity">The business object whose name is being computed. </param>
        /// <param name="strParentName">string parent name </param>
        private void SetNameForBoolFeatures(BusinessObject entity, string strParentName)
        {
            Compartment compartCreation = (Compartment)entity;
            Collection<Compartment> addFeatures = compartCreation.AddedCompartments();
            Collection<Compartment> removeFeatures = compartCreation.SubtractedCompartments();

            // 'Add '+' in the name of the added features
            foreach(Compartment addCompartment in addFeatures)
            {
                strParentName = "+ " + strParentName;
                addCompartment.SetPropertyValue(strParentName, "IJNamedItem", "Name");
            }

            // 'Add '-' in the name of the subtracted features
            foreach (Compartment subCompartment in removeFeatures)
            {
                strParentName = "- " + strParentName;
                subCompartment.SetPropertyValue(strParentName, "IJNamedItem", "Name");
            }
        }
    }
}

